*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


set level 90, perm


***DLR PAIR REGRESSIONS

***********************************************
*Regressions for period 1990-upto
local upto 2016
***********************************************

foreach u in "county" "czonestate" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

if "`u'" == "county" {
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"
keep if type0==1
drop type*
drop _merge
}

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'



***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************

egen idvar=group(`u'pair state)

gen lmw=log(mw)

tsset idvar year

* Leads and lags

forval j = 1(1)4 {
 
	gen lmw_l`j' =  L`j'.lmw
       
      gen lmw_f`j' = F`j'.lmw
     
} 

gen lmw_l0 = lmw
local uf 4
local mf 3
local lf 1
gen lmw_f`uf'_`mf'=lmw_f`uf'-lmw_f`mf'
gen lmw_f`mf'_`lf'=lmw_f`mf'-lmw_f`lf'
gen lmw_f`lf'_0=lmw_f`lf'-lmw



*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************


local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')

*USE DATA UP TO 
drop if `period'>`upto'

*Specify clustering
local options "vce(cluster bordersegment state) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" {


local yvar "``h'var'"
local yvar2 "`tot`h'var'"
local controls "`tot`h'var' `popvar'"
local controls2 "`popvar'"
local pretrends "lmw_f`mf'_`lf' lmw_f`lf'_0 lmw"
local pretrends2 "lmw_f`mf' lmw_f`lf' lmw"


eststo m1_`u': reghdfe `yvar' `pretrends' `controls', `options'
eststo m2_`u': reghdfe `yvar' `pretrends2' `controls', `options'

eststo m3_`u': reghdfe `yvar2' `pretrends' `controls2', `options'
eststo m4_`u': reghdfe `yvar2' `pretrends2' `controls2', `options'

eststo n1_`u': reghdfe `yvar' `pretrends' `controls' if pairtimes==2, `options'
eststo n2_`u': reghdfe `yvar' `pretrends2' `controls' if pairtimes==2, `options'

eststo n3_`u': reghdfe `yvar2' `pretrends' `controls2' if pairtimes==2, `options'
eststo n4_`u': reghdfe `yvar2' `pretrends2' `controls2' if pairtimes==2, `options'


}
}

/* Print results */

tempfile specs

#delimit ;
esttab m*_county m*_czonestate using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pretrends -- CBP")
	order(`pretrends' `pretrends2')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	replace;
#delimit cr

#delimit ;
esttab n*_county n*_czonestate using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pretrends -- CBP DLR pairs")
	order(`pretrends' `pretrends2')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table7_CBP)

clear all


